#!/bin/bash

SERVICE_NAME="elf-manager"
PROJECT_DIR="/mnt2/elf_virtual_manager"

case "$1" in
    start)
        echo "启动 $SERVICE_NAME 服务..."
        systemctl start $SERVICE_NAME
        ;;
    stop)
        echo "停止 $SERVICE_NAME 服务..."
        systemctl stop $SERVICE_NAME
        ;;
    restart)
        echo "重启 $SERVICE_NAME 服务..."
        systemctl restart $SERVICE_NAME
        ;;
    status)
        echo "查看 $SERVICE_NAME 服务状态..."
        systemctl status $SERVICE_NAME --no-pager
        ;;
    logs)
        echo "查看 $SERVICE_NAME 服务日志..."
        journalctl -u $SERVICE_NAME -f
        ;;
    app-logs)
        echo "查看应用日志..."
        tail -f $PROJECT_DIR/logs/app.log
        ;;
    access-logs)
        echo "查看访问日志..."
        tail -f $PROJECT_DIR/logs/access.log
        ;;
    error-logs)
        echo "查看错误日志..."
        tail -f $PROJECT_DIR/logs/error.log
        ;;
    reload)
        echo "重新加载 $SERVICE_NAME 服务配置..."
        systemctl reload $SERVICE_NAME
        ;;
    enable)
        echo "启用 $SERVICE_NAME 开机自启..."
        systemctl enable $SERVICE_NAME
        ;;
    disable)
        echo "禁用 $SERVICE_NAME 开机自启..."
        systemctl disable $SERVICE_NAME
        ;;
    update)
        echo "更新服务..."
        cd $PROJECT_DIR
        git pull
        source venv/bin/activate
        pip install -r requirements.txt
        systemctl restart $SERVICE_NAME
        echo "服务更新完成"
        ;;
    health)
        echo "检查服务健康状态..."
        curl -s http://localhost:8030/health | python3 -m json.tool
        ;;
    *)
        echo "用法: $0 {start|stop|restart|status|logs|app-logs|access-logs|error-logs|reload|enable|disable|update|health}"
        echo ""
        echo "命令说明:"
        echo "  start       启动服务"
        echo "  stop        停止服务"
        echo "  restart     重启服务"
        echo "  status      查看服务状态"
        echo "  logs        查看systemd日志"
        echo "  app-logs    查看应用日志"
        echo "  access-logs 查看访问日志"
        echo "  error-logs  查看错误日志"
        echo "  reload      重新加载配置"
        echo "  enable      启用开机自启"
        echo "  disable     禁用开机自启"
        echo "  update      更新服务"
        echo "  health      健康检查"
        exit 1
        ;;
esac